package com.nuorui.module.system.domain.vo;


import com.nuorui.common.constant.CommonConstants;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Fred
 */
public class TreeUtil {

    /**
     * 两层循环实现建树
     *
     * @param treeNodes
     * @param <T>
     * @return
     */
    public static <T extends TreeNode> List<T> build(List<T> treeNodes) {

        List<T> trees = new ArrayList<>();
        for (T treeNode : treeNodes) {
            if (CommonConstants.MENU_TREE_ROOT_ID.equals(treeNode.getParentId())) {
                trees.add(treeNode);
            }
            for (T it : treeNodes) {
                if (it.getParentId().equals(treeNode.getId())) {
                    if (treeNode.getChildren() == null) {
                        treeNode.setChildren(new ArrayList<>());
                    }
                    treeNode.add(it);
                }
            }
        }
        return trees;
    }
}
